package cn.nawang.ebeim.api.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.nawang.ebeim.api.domain.Datasource;
import cn.nawang.ebeim.api.domain.DatasourceConfig;
import cn.nawang.ebeim.api.repository.DatasourceConfigRepository;
import cn.nawang.ebeim.api.service.DatasourceConfigService;
import cn.nawang.ebeim.api.utils.Util;

/**
 * @ClassName: DatasourceConfigServiceImpl
 * @Description: TODO
 * @author fanyb
 * @date Sep 9, 2015 2:36:07 PM
 * @version v1.0
 */
@Service("datasourceConfigService")
public class DatasourceConfigServiceImpl implements DatasourceConfigService{

  @Autowired
  private DatasourceConfigRepository datasourceConfigRepository;
  
  @Override
  public boolean save(DatasourceConfig datasouceConfig) {
    try{
      datasourceConfigRepository.save(datasouceConfig);
      return true;
    }catch(Exception e){
      e.printStackTrace();
    }  
    return false;
  }

  @Override
  public boolean updateDatasourceConfig(Datasource datasource,DatasourceConfig datasouceConfig) {
    try {
      DatasourceConfig datasourceConfigByDatabase = datasourceConfigRepository.findOne(datasource.getConfigId());
      datasouceConfig = (DatasourceConfig) Util.comparisonObject(datasouceConfig, datasourceConfigByDatabase);
      datasourceConfigRepository.save(datasouceConfig);
      return true;
    } catch (Throwable e) {
      e.printStackTrace();
    }
    return false;
  }

  @Override
  public boolean delete(DatasourceConfig datasourceConfig) {
    try{
      datasourceConfigRepository.delete(datasourceConfig);
      return true;
    }catch(Exception e){
      e.printStackTrace();
    }   
    return false;
  }

}
